package com.hellpro.lolbox.home.weight;


import android.os.Handler;
import android.os.Message;
import android.widget.ListView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.hellpro.lolbox.R;
import com.hellpro.lolbox.base.BaseFragment;
import com.hellpro.lolbox.bean.VideoBean;
import com.hellpro.lolbox.config.UrlConfigs;
import com.hellpro.lolbox.home.HomeJsonUtils;
import com.hellpro.lolbox.home.model.OnZXListListener;
import com.hellpro.lolbox.home.presenter.ZXCommListAdapter;
import com.hellpro.lolbox.home.presenter.ZXFragment;
import com.hellpro.lolbox.home.view.IZXCommAFragment;

import butterknife.BindView;


public class ZXCommAFragmentB extends BaseFragment implements IZXCommAFragment {
    private String mPath = UrlConfigs.HOME_VIDEO;
    private  int page = 1;
    private int pageSize;
    @BindView(R.id.zx_commonab)
    PullToRefreshListView zxCommonab;
    private ZXCommListAdapter zxCommListAdapter;



    @Override
    public void initView() {
      zxCommonab.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
          @Override
          public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

              String path = mPath+ 1;
              goDown(path);
              zxCommonab.postDelayed(new Runnable() {
                  @Override
                  public void run() {
                      zxCommonab.onRefreshComplete();
                  }
              },7000);
          }

          @Override
          public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

              page ++;
              if (page>pageSize){
                  Toast.makeText(getFActivity(),"已到最后一页",Toast.LENGTH_SHORT).show();
                  return;
              }
              String path = mPath+ page;
              goDown(path);
              zxCommonab.postDelayed(new Runnable() {
                  @Override
                  public void run() {
                      zxCommonab.onRefreshComplete();
                  }
              },7000);
          }
      });

        zxCommListAdapter = new ZXCommListAdapter();

        zxCommonab.setAdapter(zxCommListAdapter);

                    String path = mPath + 1;
                    goDown(path);



        }




    @Override
    public int getLayoutId() {
        return R.layout.fragment_zxcomm_ab;
    }




    @Override
    public void hideProgress() {
        zxCommonab.onRefreshComplete();
    }
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what ==2){
                Object obj = msg.obj;
                VideoBean zssp = (VideoBean) obj;
              pageSize = zssp.getPageSize();
                if (zxCommonab!=null) {
                    zxCommonab.onRefreshComplete();
                }
                zxCommListAdapter.setZXData(obj,1);
            }
        }
    };


    //开启下载
    private void goDown(String path){
        new ZXFragment(getFActivity(), path, new OnZXListListener() {
            @Override
            public void loadSuccees(String json) {
                VideoBean data = HomeJsonUtils.getData(json, VideoBean.class);
                Message obtain = Message.obtain();
                obtain.what=2;
                obtain.obj= data;
                mHandler.sendMessage(obtain);

            }

            @Override
            public void loadFail() {

            }
        });

    }
}





